/*
 * Copyright 2009-2010 Andreas Veithen
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.code.jahath.service.vch;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.google.code.jahath.common.LogUtil;
import com.google.code.jahath.common.io.SwappableInputStream;
import com.google.code.jahath.common.io.SwappableOutputStream;
import com.googlecode.sisme.stream.AbstractConnection;
import com.googlecode.sisme.stream.InputStreamAdapter;
import com.googlecode.sisme.stream.OutputStreamAdapter;
import com.googlecode.sisme.stream.StreamSink;
import com.googlecode.sisme.stream.StreamSource;

class ConnectionImpl extends AbstractConnection {
    private static final Logger log = Logger.getLogger(ConnectionImpl.class.getName());
    
    private final String id;
    private final SwappableInputStream inputStream;
    private final SwappableOutputStream outputStream;
    
    ConnectionImpl(String id) {
        this.id = id;
        inputStream = new SwappableInputStream(id);
        outputStream = new SwappableOutputStream(id);
    }

    String getId() {
        return id;
    }

    @Override
    public String toString() {
        return id;
    }

    void consume(InputStream in) throws InterruptedException {
        inputStream.swap(in);
    }
    
    void produce(OutputStream out) throws InterruptedException {
        outputStream.swap(out);
    }
    
    public StreamSource getStreamSource() {
        return new InputStreamAdapter(LogUtil.log(inputStream, log, Level.FINER, "in"));
    }
    
    public StreamSink getStreamSink() {
        return new OutputStreamAdapter(LogUtil.log(outputStream, log, Level.FINER, "out"));
    }

    @Override
    protected void doClose() throws IOException {
        // TODO
    }
}
